<template>
  <div class="menu-container">
    <!-- 搜索区域 -->
    <el-card>
      <el-form :model="searchForm" inline>
        <el-form-item label="菜单名称">
          <el-input v-model="searchForm.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="全部" value="" />
            <el-option label="正常" value="0" />
            <el-option label="停用" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作按钮 -->
    <el-card style="margin-top: 20px;">
      <el-button type="primary" @click="handleAdd">新增菜单</el-button>
    </el-card>

    <!-- 菜单表格 -->
    <el-card style="margin-top: 20px;">
      <el-table :data="menuList" row-key="menuId" :tree-props="{ children: 'children' }">
        <el-table-column prop="menuName" label="菜单名称" width="200" />
        <el-table-column prop="menuType" label="类型" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.menuType === 'M' ? 'primary' : scope.row.menuType === 'C' ? 'success' : 'warning'">
              {{ getMenuTypeText(scope.row.menuType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="path" label="路由地址" width="150" />
        <el-table-column prop="component" label="组件路径" width="200" />
        <el-table-column prop="perms" label="权限标识" width="150" />
        <el-table-column prop="orderNum" label="排序" width="80" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.status === '0' ? 'success' : 'danger'">
              {{ scope.row.status === '0' ? '正常' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="160" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <MenuDialog
        v-model="dialogVisible"
        :menu-data="currentMenu"
        :mode="dialogMode"
        @success="handleDialogSuccess"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { findMenu, deleteMenu } from '@/api/menu'
import MenuDialog from './components/MenuDialog.vue'

const menuList = ref([])
const dialogVisible = ref(false)
const dialogMode = ref('add')
const currentMenu = ref(null)

const searchForm = reactive({
  menuName: '',
  status: ''
})

const getMenuTypeText = (type) => {
  const typeMap = { 'M': '目录', 'C': '菜单', 'F': '按钮' }
  return typeMap[type] || type
}

const fetchData = async () => {
  try {
    const response = await findMenu(searchForm)
    if (response.code === 200) {
      menuList.value = response.data.records || response.data
    }
  } catch (error) {
    ElMessage.error('获取菜单列表失败')
  }
}

const handleSearch = () => {
  fetchData()
}

const resetSearch = () => {
  searchForm.menuName = ''
  searchForm.status = ''
  fetchData()
}

const handleAdd = () => {
  dialogMode.value = 'add'
  currentMenu.value = null
  dialogVisible.value = true
}

const handleEdit = (row) => {
  dialogMode.value = 'edit'
  currentMenu.value = { ...row }
  dialogVisible.value = true
}

const handleDelete = (row) => {
  ElMessageBox.confirm(`确定要删除菜单 "${row.menuName}" 吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteMenu(row.menuId)
      ElMessage.success('删除成功')
      fetchData()
    } catch (error) {
      ElMessage.error('删除失败')
    }
  })
}

const handleDialogSuccess = () => {
  dialogVisible.value = false
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.menu-container {
  padding: 20px;
}
</style>
